//
//  CLVisitorView.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/9.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit
import SnapKit

class CLVisitorView: UIView {
    
    //定义登录或或者注册执行的闭包
    var loginClosure :(() ->())?
    //定义中间旋转的视图
    private lazy var cycleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮挡一半的遮罩
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //中间图片
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //注册按钮
    private lazy var registButton: UIButton = {
        //创建按钮
        let button = UIButton()
        //设置按钮的属性
        button.setTitle("注册", forState: .Normal)
        button.addTarget(self , action: "regisitButton", forControlEvents: .TouchUpInside)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState:.Normal )
        return button
    }()
    private lazy  var loginButton:UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: .Normal)
        button.addTarget(self , action: "loginButtonAction", forControlEvents: .TouchUpInside)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState:.Normal )
        return button
    }()
    //文本消息内容
    private lazy var messageLabel: UILabel = {
       let label = UILabel()
        //设置label属性
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = .Center
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0//自动换行
        return label
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI ()
    }

    //从xib或者sb中加载视图时调用
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI () {
        //设置背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        //添加属性
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registButton)
        addSubview(loginButton)
       
        //设置布局
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(10)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        registButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
            
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registButton)
        }
        
    }
    // MARK:  注册按钮点击事件
    func regisitButton ()
    {
        print("注册")
        loginClosure?()
    
        
    }
    // MARK:  登录按钮点击事件
  
    func loginButtonAction () {
        print("登录")
        loginClosure?()
    }
    // MARK:  开启旋转动画
    private func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.duration = 10
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        //失去焦点
        animation.removedOnCompletion = false
        //执行动画
        cycleImageView.layer.addAnimation(animation, forKey: nil)
    }
    // MARK:  修改访客视图
    func updateVisitorInfo (message :String? ,imageName :String?) {
        
        if let msg = message ,imgName = imageName {
            messageLabel.text = msg
            iconImageView.image = UIImage(named: imgName)
            cycleImageView.hidden = true
            
        }else {
            cycleImageView.hidden = false
            startAnimation()
        }
    }
}
